package com.yanwq.sayhi.widget.anim;

import android.support.v7.widget.RecyclerView;
import android.view.View;

import java.util.ArrayList;
import java.util.List;

/**
 * 作者：yanweiqiang。
 * 日期：2016/7/27 0027。
 */
public class RecyclerWrapper {
    private List<View> mHeaderViews;
    private List<View> mFooterViews;

    public RecyclerWrapper() {
        super();
        mHeaderViews = new ArrayList<>();
        mFooterViews = new ArrayList<>();
    }

    public void addHeader(View headerView) {
        mHeaderViews.add(headerView);
    }

    public void addFooter(View footerView) {
        mFooterViews.add(footerView);
    }

    public int getItemCount(int originCount) {
        return mHeaderViews.size() + originCount + mFooterViews.size();
    }

    public int getItemViewType(int position, int dataCount) {
        if (position < mHeaderViews.size() || position >= mHeaderViews.size() + dataCount) {
            return -1000 - position;
        }

        return -1;
    }

    public RecyclerView.ViewHolder onCreateViewHolder(int viewType, int dataCount) {
        for (int i = 0; i < mHeaderViews.size(); i++) {
            if (viewType == -1000 - i) {
                return new RecyclerView.ViewHolder(mHeaderViews.get(i)) {
                    @Override
                    public String toString() {
                        return super.toString();
                    }
                };
            }
        }

        for (int i = mHeaderViews.size() + dataCount; i < mHeaderViews.size() + dataCount + mFooterViews.size(); i++) {
            if (viewType == -1000 - i) {
                return new RecyclerView.ViewHolder(mFooterViews.get(i - mHeaderViews.size() - dataCount)) {
                    @Override
                    public String toString() {
                        return super.toString();
                    }
                };
            }
        }

        return null;
    }

    public int getOriginPosition(int position) {
        if (mHeaderViews.size() > 0) {
            return position - mHeaderViews.size();
        } else {
            return position;
        }
    }
}
